<?php
/**
 * This file is part of the jdufs project
 *
 * (c) tiglog <xq@tiglog.com>
 *
 * For the full copyright and license information, please view the LICENSE file
 * that was distributed with this source code.
 */
namespace tiglog\datatables;

use Yii;
use yii\base\InvalidConfigException;
use yii\db\ActiveQuery;

/**
 * 定义 datatables table
 *
 * @package datatables
 * @author tiglog
 */
class BaseDt implements DataInterface
{
    protected $query;


    public function __construct($query = null)
    {
        $this->query = $query;
    }

    public function initQuery()
    {

    }


    /**
     * Getter for query
     *
     * @return string
     */
    public function getQuery()
    {
        if (!$this->query) {
            $this->initQuery();
        }
        return $this->query;
    }

    public function getModelClass()
    {
        return $this->getQuery()->modelClass;
    }

    /**
     * 一般需要覆盖该方法
     * @return array
     */
    public function columns()
    {
        return [];
    }

    /**
     * 这个是整理后的 columns
     */
    public function getColumns()
    {
        $columns = [];
        foreach ($this->columns() as $k => $col) {
            if (is_string($col)) {
                $column = self::createDataColumn($col, $this);
            } else {
                $column = Yii::createObject(array_merge([
                    'class' => DataColumn::class,
                    'dt' => $this,
                ], $col));
            }
            $columns[$k] = $column;
        }
        return $columns;
    }

    /**
     * Creates a [[DataColumn]] object based on a string in the format of "attribute:format:label".
     * @param string $text the column specification string
     * @return DataColumn the column instance
     * @throws InvalidConfigException if the column specification is invalid
     */
    public static function createDataColumn($text, $ctx)
    {
        if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
            throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
        }

        return Yii::createObject([
            'class' => DataColumn::className(),
            'dt' => $ctx,
            'attribute' => $matches[1],
            'format' => isset($matches[3]) ? $matches[3] : 'text',
            'label' => isset($matches[5]) ? $matches[5] : null,
        ]);
    }


    /**
     * 这个函数用于获取数据
     */
    public function getData($params = [])
    {
        return $this->getQuery()->all();
    }

    public function onFilter(ActiveQuery $query, $columns, $search)
    {
        foreach ($this->getColumns() as $col) {
            if (!$col->isSearchAble()) {
                continue;
            }
            $col->search($query, $search['value']);
        }
        return $this->query = $query;
    }

    public function onFormatData(ActiveQuery $query, $columns)
    {
        $rows = [];
        foreach ($this->getData() as $model) {
            $row = [];
            foreach ($this->getColumns() as $col) {
                array_push($row, Yii::$app->getFormatter()->format($col->getDataCellValue($model), $col->format));
            }
            array_push($rows, $row);
        }
        return $rows;
    }

}




// vim:ft=php:ts=4:sts=4:sw=4:et
